import vjmap3d, { interpolatePointsByRatio, lineDistance, PathTubeEntities } from "vjmap3d"


// 模拟一条数据
export const mockData = (tubeEntity: PathTubeEntities) => {
    let personCount = 20; // 生成的人的总数

    let dists = tubeEntity.data.map(d => lineDistance(d.paths))
    let data: any[] = [];

    let moveDist = 10;
    // 获取数据
    const getData = () => {
        if (data.length == 0) {
            // 如果是第一次
            for(let n = 0; n < personCount; n++) {
                let idx = vjmap3d.randInt(0, tubeEntity.data.length - 1);
                let ratio = Math.random();
                let paths = tubeEntity.data[idx].paths;
                let info = {}
                let clipPoints = interpolatePointsByRatio(paths, ratio, info);
                let co = clipPoints[clipPoints.length - 1];
                // 通过这个点获取断面顶点的坐标
                let pts = tubeEntity.getSectPoints(co, idx)
                data.push({
                    // 数据索引
                    index: idx,
                    // 坐标点
                    point: pts,
                    // 当前在线段中的距离
                    curDist: dists[idx] * ratio,
                    // 每次移动的距离
                    moveDist: moveDist * ( 1+ Math.random()),
                    // 路径
                    route: [],
                    // 方向
                    dir: Math.random() < 0.5,
                    info
                })
            }
        } else {
            // 更新数据
            for(let n = 0; n < data.length; n++) {
                let item = data[n];
                let nextDist = item.curDist + item.moveDist * (item.dir ? 1 : -1);
                if (nextDist < dists[item.index] && nextDist > 0) {
                    // 还在当前线上
                    let ratio = nextDist / dists[item.index];
                    let paths = tubeEntity.data[item.index].paths;
                    let info: any = {}
                    let clipPoints = interpolatePointsByRatio(paths, ratio, info);
                    let co = clipPoints[clipPoints.length - 1];
                    // 通过这个点获取断面顶点的坐标
                    let pts = tubeEntity.getSectPoints(co, item.index)

                    // 经过的点
                    let route = [item.point];
                    if (item.dir) {
                        // 经过的中间的点
                        for(let c = item.info.curRetIndex + 1; c <= info.curRetIndex; c++) {
                            let _ps = tubeEntity.getSectPoints(tubeEntity.data[item.index].paths[c], item.index)
                            route.push(_ps)
                        }
                    } else {
                        // 经过的中间的点
                        for(let c = item.info.curRetIndex ; c >= info.curRetIndex + 1; c--) {
                            let _ps = tubeEntity.getSectPoints(tubeEntity.data[item.index].paths[c], item.index)
                            route.push(_ps)
                        }
                    }
                                
                    route.push(pts)

                    data[n] = {
                        // 数据索引
                        index: item.index,
                        // 坐标点
                        point: pts,
                        // 当前在线段中的距离
                        curDist: nextDist,
                        // 每次移动的距离
                        moveDist: item.moveDist,
                        // 路径
                        route,
                        // 方向
                        dir: item.dir,
                        info
                    }
                } else {
                    // 找到相关联的路径
                    if (nextDist > 0) {
                        nextDist = nextDist - dists[item.index];
                    } else {
                        nextDist = -nextDist;
                    }
                    
                    // 经过的点
                    let route = [item.point];
                    
                    if (item.dir) {
                        // 经过的中间的点
                        for(let c = item.info.curRetIndex + 1; c < tubeEntity.data[item.index].paths.length; c++) {
                            let _ps = tubeEntity.getSectPoints(tubeEntity.data[item.index].paths[c], item.index)
                            route.push(_ps)
                        }
                    } else {
                        // 经过的中间的点
                        for(let c = item.info.curRetIndex ; c >= 0; c--) {
                            let _ps = tubeEntity.getSectPoints(tubeEntity.data[item.index].paths[c], item.index)
                            route.push(_ps)
                        }
                    }

                    let node = item.dir ? tubeEntity.topo.tubes[item.index].endNodeId : tubeEntity.topo.tubes[item.index].startNodeId
                    // 获取节点相关联的管道
                    let nodeTubes = tubeEntity.topo.nodes.get(node) || [];
                    if (nodeTubes.length == 0) {
                        data[n].route = []
                        continue;
                    }
                    // 随机选一个做为下一个
                    let nextTubeIndex = nodeTubes[vjmap3d.randInt(1, nodeTubes?.length) - 1];
                    // 尽量不要选之前走过的路径
                    for(let k = 0; k < 3; k++)
                    {
                        if (nextTubeIndex == item.index) {
                            nextTubeIndex = nodeTubes[vjmap3d.randInt(1, nodeTubes?.length) - 1];
                        } else {
                            break;
                        }
                    }
                    item.index = nextTubeIndex;
                    if (tubeEntity.topo.tubes[nextTubeIndex].startNodeId == node) {
                        item.dir = true;
                    } else {
                        item.dir = false;
                    }

                    if (!item.dir) {
                        nextDist =  dists[item.index] - nextDist;
                    } 
                    if (nextDist >= dists[item.index] || nextDist < 0) {
                        nextDist = dists[item.index] / 2;
                    }
                    let ratio = nextDist / dists[item.index];
                    let paths = tubeEntity.data[item.index].paths;
                    let info: any = {}
                    let clipPoints = interpolatePointsByRatio(paths, ratio, info);
                    let co = clipPoints[clipPoints.length - 1];
                    // 通过这个点获取断面顶点的坐标
                    let pts = tubeEntity.getSectPoints(co, item.index)
                   
                    if (item.dir) {
                        // 经过的中间的点
                        for(let c = 0; c <= info.curRetIndex; c++) {
                            let _ps = tubeEntity.getSectPoints(tubeEntity.data[item.index].paths[c], item.index)
                            route.push(_ps)
                        }
                    } else {
                        // 经过的中间的点
                        for(let c = tubeEntity.data[item.index].paths.length - 1 ; c >= info.curRetIndex + 1; c--) {
                            let _ps = tubeEntity.getSectPoints(tubeEntity.data[item.index].paths[c], item.index)
                            route.push(_ps)
                        }
                    }
                                
                    route.push(pts)

                    data[n] = {
                        // 数据索引
                        index: item.index,
                        // 坐标点
                        point: pts,
                        // 当前在线段中的距离
                        curDist: nextDist,
                        // 每次移动的距离
                        moveDist: item.moveDist,
                        // 路径
                        route,
                        // 方向
                        dir: item.dir,
                        info
                    }

                }
            }
        }
        return data;
    }

    return {
        getData
    }
}